package com.api.action.v1.pms;

import ch.qos.logback.core.net.SyslogOutputStream;
import cn.hutool.http.HttpUtil;
import com.admin.entity.Station;
import com.admin.entity.Terminal;
import com.admin.service.impl.StationServiceImpl;
import com.admin.service.impl.TerminalServiceImpl;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 绿云
 *
 */
@RestController
@RequestMapping("/api/greencloud/v2/")
public class GreenCloudAction2 {

    @Autowired
    private StationServiceImpl stationService;

    @Autowired
    private TerminalServiceImpl terminalService;

    @PostMapping("/postPms")
    public Map post(@RequestBody String content)
    {
        System.out.println(content);
        //{"rescode":"200","resDesc":"访问成功"}
        Map map = new HashMap();
        map.put("rescode",200);
        map.put("resDesc","访问成功");

        JSONObject jsonObject = JSONObject.parseObject(content);
        String hotelCode = jsonObject.getString("hotelCode");  //酒店代码
        String  bizType   = jsonObject.getString("bizType");  //业务类型
        String  name      = jsonObject.getString("name");     //顾客名字
        String  rmno      = jsonObject.getString("rmno");//房号
        String  hotelDescript = jsonObject.getString("hotelDescript"); //酒店名称
        String  sex   = jsonObject.getString("sex");
        sex = "1".equals(sex)? "先生":"女士";

        Station station  = stationService.findByPms(hotelCode);
        if (station==null) {
            station = stationService.findByPms(hotelDescript);
        }
        if (station==null){
            map.put("resDesc","酒店代码没有设置");
            return map;
        }
        Terminal t = terminalService.getTerminalByRoom(station,rmno);
        if (t==null){
            map.put("resDesc","房间号不存设置");
            return map;
        }
        switch (bizType){
            case "CHECKIN":  //入住
            case "GUESTCHG": //修改信息
            case "RMCHG": //换房
                t.setTerminalPmsName(name+sex);
                break;
            case "CHECKOUT": //退房
                t.setTerminalPmsName(null);
                break;
        }

        terminalService.updateById(t);
        return map;
    }


    public static void main(String[] args) {
        String url = "https://bms.mehoodhotels.com/api/ai/hotel/";
        //url = "https://api.wlank.com/api/Pms/GreenCloud";
        String key = "13kGFJERFED4EW5R2DF4S4E3";
        String hotelID="185";
        String room = "all";
        //String sing = "hotelId=值&key=值&rmno=值组合后进行md5";
        StringBuffer sb = new StringBuffer();
        sb.append("hotelId=").append(hotelID);
        sb.append("&key=").append(key);
        sb.append("&rmno=").append(room);
        String sign = MD5Util.getMD5(sb.toString()).toLowerCase();
        //System.out.println(sign);
        Map map = new HashMap();
        map.put("id",hotelID);
        map.put("room",room);
        map.put("sign",sign);
        String result = HttpUtil.get(url,map);
        System.out.println(result);

        JSONObject jsonObjectRoot = JSONObject.parseObject(result);
        if (jsonObjectRoot!=null){
            int status = jsonObjectRoot.getIntValue("status");
            if (status==0)
            {
                JSONArray jsonArray = jsonObjectRoot.getJSONArray("result");
                if (jsonArray!=null)
                {

                    for (int i=0;i<jsonArray.size();i++){
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        JSONObject jsonObjectGuest = jsonObject.getJSONObject("guests");
                        if (jsonObjectGuest==null) continue;
                        String name = jsonObjectGuest.getString("name");
                        String rmno = jsonObject.getString("rmno");
                        String idNo = jsonObjectGuest.getString("idNo");

                        String _sex = "";
                        if (idNo!=null && idNo.trim().length()>=18){
                            String sex = idNo.substring(16,17);
                            if (Integer.parseInt(sex)%2==1){
                                _sex = "先生";
                            }
                            else{
                                _sex ="女士";
                            }

                            Date depTime = jsonObjectGuest.getSqlDate("dep");

                            if (depTime.getTime()<System.currentTimeMillis()){
                                _sex = "离开";
                            }
                        }
                        System.out.println(idNo+"\t"+name+_sex+"\t"+rmno);
                    }
                }

            }
        }

    }


}
